package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.blog.dao.mapper.CommentMapper;
import com.example.blog.entity.Comment;
import com.example.blog.entity.SysUser;
import com.example.blog.service.CommentsService;
import com.example.blog.service.SysUserService;
import com.example.blog.utils.UserThreadLocal;
import com.example.blog.vo.CommentVo;
import com.example.blog.vo.Result;
import com.example.blog.vo.UserVo;
import com.example.blog.vo.params.CommentParam;
import com.mysql.jdbc.util.ResultSetUtil;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 1.1.需要建立CommentsVo和UserVo，与前端对应
     * 1.2.根据文章id查询评论列表，从comments表中查询(首先只查level为1的评论)
     * 2.根据作者id查询作者信息
     * 3.判断如果level=1，查询它有没有子评论
     * 4.如果有，根据评论id(parentsId)查询子评论
     *
     * @param id
     * @return
     */
    @Override
    public Result commentsByArticleId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getLevel, 1);
        queryWrapper.eq(Comment::getArticleId, id);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        return Result.success(copyList(comments));
    }

    /**
     * 评论
     *
     * @param commentParam
     * @return
     */
    @Override
    public Result comment(CommentParam commentParam) {
        //评论必须登录成功,所以可以拿到当前用户信息
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setAuthorId(sysUser.getId());
        comment.setArticleId(commentParam.getArticleId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());

        Long parentId = commentParam.getParentId();
        //前端传递的parentId为0或者null，代表它就是父评论
        if (parentId == null || parentId == 0) {
            comment.setLevel(1);
        } else
            comment.setLevel(2);
        comment.setParentId(parentId == null ? 0 : parentId);

        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        //如果toUserId为null，代表评论的是文章,而不是评论
        commentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        commentVo.setId(String.valueOf(comment.getId()));
        /*还剩author、children、createDate、toUser*/

        //作者信息author
        Long authorId = comment.getAuthorId();
        UserVo userVo = sysUserService.findUserVoByAuthorId(authorId);
        commentVo.setAuthor(userVo);

        //子评论children
        Integer level = comment.getLevel();
        if (1 == level) {
            Long id = comment.getId();
            List<CommentVo> commentVoList = findCommentsByCommentId(id);
            commentVo.setChildren(commentVoList);
        }

        //createDate
        commentVo.setCreateDate(new DateTime(comment.getCreateDate()).toString("yyyy-MM-dd HH:mm"));

        //给评论的哪个作者评论的 toUser
        if (level > 1) {
            Long toUid = comment.getToUid();
            UserVo toUserVo = sysUserService.findUserVoByAuthorId(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    /**
     * 通过commentId查询对应的子评论
     * 对comment表一一遍历 如果谁的parentId等于该评论的id，并且level等于2，加入到list中
     *
     * @param id
     * @return
     */
    private List<CommentVo> findCommentsByCommentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId, id);
        queryWrapper.eq(Comment::getLevel, 2);//只有level为2的才会有父评论
        return copyList(commentMapper.selectList(queryWrapper));
    }
}
